package edu.udel.mboch.towerdefense;

import java.io.Serializable;

public class Projectile implements Serializable
{
	private static final long serialVersionUID = 5176625800919245632L;
	private float speed;
	private FloatingPosition position;
	private Mob targetMob;
	private long timer;
	private Tower tower;
	
	public Projectile(Tower tower, Mob targetMob)
	{
		this.tower = tower;
		speed = 150;
		position = new FloatingPosition(tower.getPosition().x() + .5f, tower.getPosition().y() + .5f);
		this.targetMob = targetMob;
		timer = 0;
	}
	
	public Projectile(FloatingPosition position, Tower tower, Mob targetMob, long timer)
	{
		this.tower = tower;
		speed = 150;
		this.position = position;
		this.targetMob = targetMob;
		this.timer = timer;
	}
	
	public FloatingPosition getPosition()
	{
		return position;
	}
	
	public void addTime(long time)
	{
		this.timer += time;
	}
	
	public void advanceProjectile(TDState state)
	{
		//I know this code isn't optimized and doesn't really work coorectly. However, I really liked the way it made the projectiles move so I kinda just kept it
		if(!state.getGrid().mobIsOnGrid(targetMob))
		{
			state.getGrid().removeProjectile(this);
		}
		else
		{
			float x = targetMob.getPosition().x() + .5f;
			float y = targetMob.getPosition().y() + .5f;
			float[] vector = position.vectorTowards(new FloatingPosition(x, y));
			position = new FloatingPosition((float)(position.x() + vector[0]*timer/speed), (float)(position.y() + vector[1]*timer/speed));
			if(vector[0] == -1 && position.x() < x)
			{
				if(vector[1] == -1 && position.y() < y)
				{
					position.y(y);
					position.x(x);
				}
				else if(vector[1] == 1 && position.y() > y)
				{
					position.y(y);
					position.x(x);
				}
				else if(vector[1] == 0 && position.y() == y)
				{
					position.x(x);
				}
			}
			else if(vector[0] == 1 && position.x() > x)
			{
				if(vector[1] == -1 && position.y() < y)
				{
					position.y(y);
					position.x(x);
				}
				else if(vector[1] == 1 && position.y() > y)
				{
					position.y(y);
					position.x(x);
				}
				else if(vector[1] == 0 && position.y() == y)
				{
					position.x(x);
				}
			}
			else if(vector[0] == 0 && position.x() == x)
			{
				if(vector[1] == -1 && position.y() < y)
				{
					position.y(y);
				}
				else if(vector[1] == 1 && position.y() > y)
				{
					position.y(y);
				}
				else if(vector[1] == 0 && position.y() == y)
				{
				}
			}
			if(Math.abs(position.y()-y)<=.05 && Math.abs(position.x()-x)<=.05)
			{
				state.getGrid().removeProjectile(this);
				targetMob.damage(tower.getDamage(), state);
				if(tower instanceof SlowTower)
					targetMob.slow();
				else if(tower instanceof SplashTower)
				{
					Mob[] mobs = new Mob[state.getGrid().getMobs().size()];
					for(int i = 0; i<mobs.length; i++)
						mobs[i] = state.getGrid().getMobs().get(i);
					for(Mob m : mobs)
					{
						
						if(Math.sqrt(Math.pow(m.getPosition().x()-targetMob.getPosition().x(), 2)+Math.pow(m.getPosition().y()-targetMob.getPosition().y(),2)) <= 1.5)
							m.damage((int)(tower.getDamage()*.75), state);
					}
				}
			}
			timer = 0;
		}
	}
	
	public Projectile copy()
	{
		return new Projectile(position.copy(), tower.copy(), targetMob.copy(), timer);
	}
}
